#include "net_defs.h"

#include <stdio.h>
#include <string.h>

static kv_t action_map[] = {
    { ACTION_INVALID, "INVALID" },
    { ACTION_DROP, "DROP" },
    { ACTION_ACCEPT, "ACCEPT" },
    { ACTION_REJECT, "REJECT" },
};

static kv_t proto_map[] = {
    { PROTO_INVALID, "INVALID" },
    { PROTO_TCP, "tcp" },
    { PROTO_UDP, "udp" },
    { PROTO_ICMP, "icmp" },
};

uint32_t get_key_by_val(kv_t map[], int len, char* val)
{
    for (int i = 0; i < len; i++) {
        kv_t e = map[i];
        if (0 == strcmp(val, e.val)) {
            return e.key;
        }
    }
    return 0;
}

char* get_val_by_key(kv_t map[], int len, uint32_t key)
{
    for (int i = 0; i < len; i++) {
        kv_t e = map[i];
        if (key == e.key) {
            return e.val;
        }
    }
    return "UNKONW";
}

uint32_t get_action(char* action)
{
    return get_key_by_val(action_map, NELM_KV(action_map), action);
}

uint32_t get_proto(char* proto)
{
    return get_key_by_val(proto_map, NELM_KV(proto_map), proto);
}

const char* get_action_str(ACTION_TYPE action)
{
    return get_val_by_key(action_map, NELM_KV(action_map), action);
}

void print_pkt_md(pkt_metadata_t* pkt_md, const char* reason)
{
    if (!pkt_md || !reason)
        return;

    printf("%s:\n", reason);
    printf("\tpkt_metadat.src: %s\n", pkt_md->src);
    printf("\tpkt_metadat.dst: %s\n", pkt_md->dst);
    int i = 0;
    while (pkt_md->sport[i]) {
        printf("\tpkt_metadat.sport[%d]: %d\n", i, pkt_md->sport[i]);
        i++;
    }
    i = 0;
    while (pkt_md->dport[i]) {
        printf("\tpkt_metadat.dport[%d]: %d\n", i, pkt_md->dport[i]);
        i++;
    }
    printf("\tpkt_metadat.proto: %d\n", pkt_md->proto);
    printf("\tpkt_metadat.action: %d\n", pkt_md->action);
    printf("\tpkt_metadat.bitmap: %d\n", pkt_md->bitmap);
}